package logic

import (
	"context"
	"fmt"
	"home-manage/model"

	"home-manage/homerpc/homerpc"
	"home-manage/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *homerpc.LoginRequest) (*homerpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user model.User
	model.DB.Debug().Where("tel=?", in.Tel).Find(&user)
	if user.ID == 0 {

		user = model.User{
			Tel:      in.Tel,
			Password: in.Password,
		}
		model.DB.Debug().Create(&user)

	}
	if user.ID != 0 {

		if in.Password != user.Password {
			return nil, fmt.Errorf("密码错误请重新输入")
		}
	}
	return &homerpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
